<template name="device">
	<view>
		<u-picker :show="deviceShow" ref="uPicker" :columns="deviceList" @confirm="confirmsds" @change="changeHandler"  @cancel="deviceShow = false"></u-picker>
	</view>
</template>

<script>
	export default {
		name:"device-linkage",
		data() {
			return {
				deviceShow: false,
				deviceList: [],
				columnData:[],
				param:{}
			};
		},
		created() {
			this.findAllMerchantdeviceByUserId();
		},
		methods: {
			//打开设备弹框
			open() {
				this.deviceShow = true;
			},
			//确认
			confirmsds(e){
				//截取设备编号
			 	let deviceNo = e.value[1].substring(0,e.value[1].indexOf('号'));
				let param = {
					name:e.value[1],
					deviceNo:deviceNo
				}
				this.$emit('deviceData',param);
				this.deviceShow = false;
			},
			changeHandler(e){
				console.log(e,"eeeeeeeee")
				const {
					columnIndex,
					value,
					values, // values为当前变化列的数组内容
					index,
					// 微信小程序无法将picker实例传出来，只能通过ref操作
					picker = this.$refs.uPicker
				} = e
				// 当第一列值发生变化时，变化第二列(后一列)对应的选项
				if (columnIndex === 0) {
					// picker为选择器this实例，变化第二列对应的选项
					picker.setColumnValues(1, this.columnData[index])
				}
			},
			findAllMerchantdeviceByUserId() {
				this.$api('device.findDevice', this.param).then(res => {
					if (res.status == 0) {
						let newArr = [];
						let newArr2 = [];
						//过滤重复的场地
						for (let i = 0; i < res.data.length; i++) {
							if (newArr.indexOf(res.data[i].storeName) === -1) {
								newArr.push(res.data[i].storeName)
							}
							//默认选中第一个场地对应的机器编号
							if(res.data[i].storeName == newArr[0]){
								newArr2.push(res.data[i].deviceNo + '号' + res.data[i].deviceTypeName)
							}
						}
						//场地对应的机器和编号
						for (let i = 0; i < newArr.length; i++) {
							let data = [];
							res.data.map((item,index)=>{
								if(item.storeName == newArr[i]){
									data.push(item.deviceNo + '号' + item.deviceTypeName)
								}
							})
							this.columnData.push(data);
						}
						this.deviceList.push(newArr)
						this.deviceList.push(newArr2)
					} else {
						this.$helpers.toast('请求数据失败:' + res.data.msg);
					}
				});
			},
		}
	}
</script>

<style>

</style>